package com.artpri.action.user;


import java.io.ByteArrayInputStream;

import com.artpri.util.SecurityCode;
import com.artpri.util.SecurityCode.SecurityCodeLevel;
import com.artpri.util.SecurityImage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 生成验证码
 * @author ZhangSi
 */
public class SecurityCodeImageAction extends ActionSupport {

	
	private static final long serialVersionUID = 8425362370979352667L;
	// 图片流
	private ByteArrayInputStream imageStream;

	@Override
	public String execute() throws Exception {
		// 如果开启Hard模式，可以不区分大小写
		String securityCode = SecurityCode.getSecurityCode(4, SecurityCodeLevel.Hard, false).toLowerCase();
		
		System.out.println("验证码: " + securityCode);
		
		// 获取默认难度和长度的验证码
		//String securityCode = SecurityCode.getSecurityCode();
		imageStream = SecurityImage.getImageAsInputStream(securityCode);
		// 放入session中
		ActionContext.getContext().getSession().put("securityCode", securityCode);
		return SUCCESS;
	}

	
	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}

}
